//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "FdmLinearOpIterator.h"
using namespace Cephei::QL::Experimental::Finitedifferences;
#undef HANDLE
#undef ABSTRACT
#define STRUCT
Cephei::QL::Experimental::Finitedifferences::CFdmLinearOpIterator::CFdmLinearOpIterator (Cephei::IVector<UInt64>^ dim, Cephei::IVector<UInt64>^ coordinates, UInt64 index) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CoVector<UInt64>^ _Cdim;
    CoVector<UInt64>^ _Ccoordinates;
    try
    {
#ifdef HANDLE
        _phFdmLinearOpIterator = NULL;
#endif
        CoVector<UInt64>^ _Cdim = safe_cast<CoVector<UInt64>^> (dim);
        _Cdim->Lock();
        INativeVector<UInt64>^ _NCIdim = _Cdim->getFeature (NativeFeature::Value);
        CUInt64Vector^ _NCdim = safe_cast<CUInt64Vector^>(_NCIdim);
        std::vector<QuantLib::Size>& _dim = static_cast<std::vector<QuantLib::Size>&> (_NCdim->GetReference ());
        CoVector<UInt64>^ _Ccoordinates = safe_cast<CoVector<UInt64>^> (coordinates);
        _Ccoordinates->Lock();
        INativeVector<UInt64>^ _NCIcoordinates = _Ccoordinates->getFeature (NativeFeature::Value);
        CUInt64Vector^ _NCcoordinates = safe_cast<CUInt64Vector^>(_NCIcoordinates);
        std::vector<QuantLib::Size>& _coordinates = static_cast<std::vector<QuantLib::Size>&> (_NCcoordinates->GetReference ());
        QuantLib::Size _index = (QuantLib::Size)ValueHelper::Convert (index);
        _ppFdmLinearOpIterator = new boost::shared_ptr<QuantLib::FdmLinearOpIterator> (new QuantLib::FdmLinearOpIterator ( _dim,  _coordinates,  _index ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cdim != nullptr) _Cdim->Unlock();
        if (_Ccoordinates != nullptr) _Ccoordinates->Unlock();
    }
}
Cephei::QL::Experimental::Finitedifferences::CFdmLinearOpIterator::CFdmLinearOpIterator (Cephei::IVector<UInt64>^ dim) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CoVector<UInt64>^ _Cdim;
    try
    {
#ifdef HANDLE
        _phFdmLinearOpIterator = NULL;
#endif
        CoVector<UInt64>^ _Cdim = safe_cast<CoVector<UInt64>^> (dim);
        _Cdim->Lock();
        INativeVector<UInt64>^ _NCIdim = _Cdim->getFeature (NativeFeature::Value);
        CUInt64Vector^ _NCdim = safe_cast<CUInt64Vector^>(_NCIdim);
        std::vector<QuantLib::Size>& _dim = static_cast<std::vector<QuantLib::Size>&> (_NCdim->GetReference ());
        _ppFdmLinearOpIterator = new boost::shared_ptr<QuantLib::FdmLinearOpIterator> (new QuantLib::FdmLinearOpIterator ( _dim ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cdim != nullptr) _Cdim->Unlock();
    }
}
Cephei::QL::Experimental::Finitedifferences::CFdmLinearOpIterator::CFdmLinearOpIterator (boost::shared_ptr<QuantLib::FdmLinearOpIterator>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmLinearOpIterator = NULL;
#endif
	_ppFdmLinearOpIterator = &childNative;
    
}
Cephei::QL::Experimental::Finitedifferences::CFdmLinearOpIterator::CFdmLinearOpIterator (QuantLib::FdmLinearOpIterator& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phFdmLinearOpIterator = NULL;
#endif
	_ppFdmLinearOpIterator = new boost::shared_ptr<QuantLib::FdmLinearOpIterator> (&childNative);
    
    _FdmLinearOpIteratorOwner = owner;
    
}

Cephei::QL::Experimental::Finitedifferences::CFdmLinearOpIterator::CFdmLinearOpIterator (CFdmLinearOpIterator^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmLinearOpIterator = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppFdmLinearOpIterator = new boost::shared_ptr<QuantLib::FdmLinearOpIterator> (copy->GetShared());
        
    }
}
Cephei::QL::Experimental::Finitedifferences::CFdmLinearOpIterator::CFdmLinearOpIterator (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmLinearOpIterator = NULL;
#endif
	if (!t->IsSubclassOf(CFdmLinearOpIterator::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Finitedifferences::CFdmLinearOpIterator::CFdmLinearOpIterator (QuantLib::Handle<QuantLib::FdmLinearOpIterator>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phFdmLinearOpIterator = &childNative;
	_ppFdmLinearOpIterator = &static_cast<boost::shared_ptr<QuantLib::FdmLinearOpIterator>>(childNative.currentLink());
    
    _FdmLinearOpIteratorOwner = owner;
}
Cephei::QL::Experimental::Finitedifferences::CFdmLinearOpIterator::CFdmLinearOpIterator (QuantLib::Handle<QuantLib::FdmLinearOpIterator> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phFdmLinearOpIterator = &childNative;
	_ppFdmLinearOpIterator = &static_cast<boost::shared_ptr<QuantLib::FdmLinearOpIterator>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Finitedifferences::CFdmLinearOpIterator::CFdmLinearOpIterator (QuantLib::FdmLinearOpIterator childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmLinearOpIterator = NULL;
#endif
	_ppFdmLinearOpIterator = new boost::shared_ptr<QuantLib::FdmLinearOpIterator> (new QuantLib::FdmLinearOpIterator (childNative));
    
}
#endif

Cephei::QL::Experimental::Finitedifferences::CFdmLinearOpIterator::~CFdmLinearOpIterator ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppFdmLinearOpIterator != NULL)
    {
	    delete _ppFdmLinearOpIterator;
        _ppFdmLinearOpIterator = NULL;
    }
}
Cephei::QL::Experimental::Finitedifferences::CFdmLinearOpIterator::!CFdmLinearOpIterator ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppFdmLinearOpIterator != NULL)
    {
	    delete _ppFdmLinearOpIterator;
    }
}
QuantLib::FdmLinearOpIterator& Cephei::QL::Experimental::Finitedifferences::CFdmLinearOpIterator::GetReference ()
{
    if (_ppFdmLinearOpIterator == NULL) throw gcnew NativeNullException ();
	return **_ppFdmLinearOpIterator;
}
boost::shared_ptr<QuantLib::FdmLinearOpIterator>& Cephei::QL::Experimental::Finitedifferences::CFdmLinearOpIterator::GetShared ()
{
    if (_ppFdmLinearOpIterator == NULL) throw gcnew NativeNullException ();
	return *_ppFdmLinearOpIterator;
}
QuantLib::FdmLinearOpIterator* Cephei::QL::Experimental::Finitedifferences::CFdmLinearOpIterator::GetPointer ()
{
    if (_ppFdmLinearOpIterator == NULL) throw gcnew NativeNullException ();
	return &**_ppFdmLinearOpIterator;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::FdmLinearOpIterator>& Cephei::QL::Experimental::Finitedifferences::CFdmLinearOpIterator::GetHandle ()
{
	if (_phFdmLinearOpIterator == NULL)
	{
		_phFdmLinearOpIterator = new Handle<QuantLib::FdmLinearOpIterator> (*_ppFdmLinearOpIterator);
	}
	return *_phFdmLinearOpIterator;
}
#endif
bool Cephei::QL::Experimental::Finitedifferences::CFdmLinearOpIterator::HasNative () 
{
	return (_ppFdmLinearOpIterator != NULL);
}

Cephei::IVector<UInt64>^ Cephei::QL::Experimental::Finitedifferences::CFdmLinearOpIterator::Coordinates::get ()
{
    try
    {
    	std::vector<QuantLib::Size> & _rv = (std::vector<QuantLib::Size> &)(*_ppFdmLinearOpIterator)->coordinates ( );   
        Cephei::IVector<UInt64>^ _nrv = gcnew CoVector<UInt64> (gcnew CUInt64Vector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Experimental::Finitedifferences::CFdmLinearOpIterator::Index::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppFdmLinearOpIterator)->index ( );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Experimental::Finitedifferences::IFdmLinearOpIterator^ Cephei::QL::Experimental::Finitedifferences::CFdmLinearOpIterator::Swap (Cephei::QL::Experimental::Finitedifferences::IFdmLinearOpIterator^ iter)
{
    CFdmLinearOpIterator^ _Citer;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Citer = safe_cast<CFdmLinearOpIterator^> (iter);
        _Citer->Lock();
        QuantLib::FdmLinearOpIterator& _iter = static_cast<QuantLib::FdmLinearOpIterator&> (_Citer->GetReference ()); 
    	(*_ppFdmLinearOpIterator)->swap ( _iter );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Citer != nullptr) _Citer->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Finitedifferences::IFdmLinearOpIterator^ Cephei::QL::Experimental::Finitedifferences::CFdmLinearOpIterator_Factory::Create (Cephei::IVector<UInt64>^ dim, Cephei::IVector<UInt64>^ coordinates, UInt64 index)
{
    return gcnew CFdmLinearOpIterator ( dim,  coordinates,  index);
}
Cephei::QL::Experimental::Finitedifferences::IFdmLinearOpIterator^ Cephei::QL::Experimental::Finitedifferences::CFdmLinearOpIterator_Factory::Create (Cephei::IVector<UInt64>^ dim)
{
    return gcnew CFdmLinearOpIterator ( dim);
}
